/**
 * zmn模块模块-模块列表
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author heguanghua
 * @since 2020/04/16 14:50
 */
let editObj = null, ptable = null, treeGrid = null, tableId = 'zmn-table', layer = null;
layui.define(['form', 'table', 'zmnApp', 'treeGrid'], function (exports) {
    let $ = layui.jquery;
    let form = layui.form;
    let layer = layui.layer;
    let treeGrid = layui.treeGrid;
    let zmnApp = layui.zmnApp;
    let table = layui.table;

    ptable = treeGrid.render({
        id: tableId,
        elem: '#' + tableId,
        url: zmnApp.SYS_BASE_URL + '/mcc/menu/ajaxTree.action?'
            + ((json.keyword == null || json.keyword === '') ? '' : ('keyword=' + json.keyword + "&"))
            + ((json.menuUrl == null || json.menuUrl === '') ? '' : ('menuUrl=' + json.menuUrl + '&'))
            + ((json.status == null || json.status === '') ? '' : ('status=' + json.status + '&')),
        cellMinWidth: 100,
        idField: 'menuId',                //必須字段
        treeId: 'menuId',                 //树形id字段名称
        treeUpId: 'parentId',               //树形父id字段名称
        treeShowName: 'menuName',         //以树形式显示的字段
        heightRemove: [".dHead", 123],      //不计算的高度,表格设定的是固定高度，此项不生效
        height: '100%',
        isFilter: false,
        iconOpen: false,                    //是否显示图标【默认显示】
        isOpenDefault: true,                //节点默认是展开还是折叠【默认展开】
        loading: true,
        method: 'POST',
        isPage: false,
        cols: [[
            {
                field: '', title: '选择', width: 50, align: 'center', sort: false, templet: function (d) {
                    return '<input type="checkbox" name="layTableCheckbox" value="' + d.LAY_INDEX + '" lay-skin="primary">';
                }
            },
            {field: 'sort', title: '排序', width: 60, align: 'center', sort: false, edit: 'text'},
            {field: 'menuId', title: '菜单ID', width: 80, align: 'center', sort: false},
            {
                field: 'menuName', title: '菜单名称', minWidth: 230, sort: false, templet: function (d) {
                    return zmnTpl.linkTpl(d.menuName, zmnApp.SYS_BASE_URL + '/mcc/menu/addEditView.action?parentId=' + d.parentId + '&menuId=', d.menuId);
                }
            },
            {field: 'menuIcon', title: '菜单icon', width: 160, align: 'center', sort: false},
            {field: 'menuUrl', title: '菜单URL', minWidth: 260, sort: false},
            {
                field: 'status', title: '状态', width: 100, align: 'center', sort: false, templet: function (d) {
                    return zmnTpl.statusTpl(d.status);
                }
            },
            {
                field: '操作', title: '操作', width: 140, align: 'left', templet: function (d) {
                    let html = '';
                    html += '<a lay-href="' + json.sysMcc + '/mcc/menu/addEditView.action?menuId=' + (d.menuId === undefined ? 0 : d.menuId) + (d.parentId === undefined ? '' : ('&parentId=' + d.parentId)) + '"'
                        + ' class="layui-btn layui-btn-xs layui-btn-primary" >详情</a>';
                    if (d.menuType == 1) {
                        html += '<a lay-href="' + json.sysMcc + '/mcc/menu/addEditView.action?parentId=' + (d.menuId === undefined ? 0 : d.menuId) + '"'
                            + ' class="layui-btn layui-btn-xs layui-btn-primary" >+子菜单</a>';
                    } else {
                        html += '<a lay-href="' + filterSysUrl(d.sysId) + d.menuUrl + '" class="layui-btn layui-btn-normal layui-btn-xs" lay-text="' + d.menuName + '" title="跳转"><i class="layui-icon">&#xe62c;</i></a>';
                    }
                    return html;
                }
            }
        ]],
        parseData: function (res) {     // 数据加载后回调
            res.data.forEach(function (item) {
                if (item.level > 0) {
                    item.lay_is_open = false;
                }
            });
            return {"message": res.message, "code": res.status, "data": res.data, "count": 0, "is": true, "tip": "OK"};
        },
    });

    // 修改排序
    treeGrid.on("edit(zmn-table)", function (obj) {
        let url = zmnApp.SYS_BASE_URL + "/mcc/menu/modify/updateSort.action";
        zmnPage.laySort(obj, url, obj.data.menuId);
    });

    // 启用/停用（批量）
    setStatus = function (status) {

        let $ = layui.$;
        let checkStatus = treeGrid.checkStatus('zmn-table');
        let ids = new Array();
        checkStatus.data.forEach(function (row, i) {
            ids.push(row['menuId']);
        });
        if (ids == null || ids.length == 0) {
            layui.layer.msg("请选择要更改状态的模块");
            return;
        }
        zmnPage.ajaxSimple(zmnApp.SYS_BASE_URL + "/mcc/menu/modify/updateStatus.action", {
            ids: ids.join(","),
            status: status
        }, 2000);
    };

    // 获取sys域名
    function filterSysUrl(sysId) {

        if (sysId === undefined || sysId == null) {
            return '';
        }
        let targetSys = json.sysList.filter(sys => sys.sysId === sysId);
        if (targetSys === undefined || targetSys == null || targetSys.length !== 1) {
            return '';
        } else {
            return targetSys[0].url;
        }

    }


    //对外暴露的接口
    exports('mcc_menu_tree', {});
});